package com.geekaca.web.servlet;

import com.alibaba.fastjson.JSON;
import com.geekaca.web.service.BrandService;
import com.geekaca.web.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/delete")
public class DelServlet extends HttpServlet {
    private static final Logger LOGGER = LoggerFactory.getLogger(DelServlet.class);

    private BrandService brandService = new BrandService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("delete servlet...");
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type", "text/html; charset=utf-8");
        String idstr = req.getParameter("id");
        Integer id = Integer.parseInt(idstr);
        LOGGER.info("service : " + id);

        int i = brandService.deleteBrand(id);
        Result result = new Result();
        PrintWriter writer = resp.getWriter();

        if (i > 0) {
            result.setCode(200);
            result.setMsg("删除成功");
            String s = JSON.toJSONString(result);
            writer.write(s);
            writer.flush();
        } else {
            result.setCode(400);
            result.setMsg("删除失败");
            String s = JSON.toJSONString(result);
            writer.write(s);
            writer.flush();
        }


    }
}
